const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const devServer = require('./webpack.devserver');
const { arrHTML, entry } = require('./webpack.html');

module.exports = {
  mode: 'development',
  entry,
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: 'js/[id]_bundle.js',
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
          },
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  [
                    'postcss-preset-env'
                  ],
                ],
              }
            }
          }
        ]
      },
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env'
              ]
            }
          }
        ]
      },
      {
        test: /\.(jpg|png|gif|jpeg|bmp)$/,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 1024
          }
        },
        generator: {
          filename: 'images/[contenthash][ext]'
        }
      },
      {
        test: /\.(woff|eot|ttf|woff2)$/,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[contenthash][ext]'
        }
      },
      {
        test: /\.(htm|html)$/,
        use: [
          'raw-loader'
        ]
      },
    ]
  },
  optimization: {
    minimizer: [
      new CssMinimizerPlugin()
    ],
    minimize: true,
    runtimeChunk: 'single'
  },
  plugins: [
    ...arrHTML,
    new MiniCssExtractPlugin({
      filename: 'css/main_[id].css'
    })
  ],
  target: 'web',
  devServer
}